<?php
/**
 * infocontroller.php
 * Create on 2015-06-08
 * Create by yuebin
 */
namespace haibao\cms\business\control\wap\common;

use haibao\cms\business\control\ArticleController;
use \haibao\cms\business\control\pc\entity\WapInfoEntity as WapInfoEntity;

class InfoController extends \haibao\cms\business\control\pc\channel\ChannelController{

	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){
		
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$path = $this->generateHtmlPath(__CLASS__);
		

		if($controlData['manual']){
			$this->parseTagId($this->getParameter(self::$SITE_CHANNEL_TAG));
			if($this->conditionTagId == 955){
			    $this->conditionTagId = array(955,2848);
			}
			$dataCondition = $controlData['manual'];
			$dataCondition['currentPage'] = $this->getParameter(self::$CURRENTPAGE);
			$this->getInfoList($dataCondition);
		}
		
		$this->assign('currentPage',$this->getParameter(self::$CURRENTPAGE));
		$this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
	/**
	 * 获取资讯列表
	 */
	private function getInfoList($dataCondition){
	    $channelType = '';
	    $infoListq = array();
// 	    if(isset($dataCondition['channelType'])){
// 	        $channelType = $dataCondition['channelType'];
// 	    }
// 	    $this->conditionTagId = $channelType;
	    $wapInfoEntity = new WapInfoEntity();
	    if($this->conditionTagId !=\haibao\cms\business\control\PublishController::NO_STRUCTURE_TAG && $this->conditionTagId != 0){
	        $wapInfoEntity->setIsArticleTag(true);
	        $wapInfoEntity->setTagIds($this->conditionTagId);
	    }else{
	        $wapInfoEntity->setIsArticleTag(false);
	    }
	    $wapInfoEntity->setIsProImage(true);
	    $wapInfoEntity->setIsImageColl(true);
	    $wapInfoEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
	    $wapInfoEntity->setSource(__CLASS__.$this->conditionTagId);
        $wapInfoEntity->setLimit($dataCondition['limit']);
        $wapInfoEntity->setCurrentPage($dataCondition['currentPage']);
	    $this->wapInfoAction->setWapInfoEnity($wapInfoEntity);
	    list($infos,$articleInfoArr,$articleProImage,$imageCollInfoArr,$collProImage,$articleTagArr,$collTagArr,$getArticleInTags,$videoProImages) = $this->wapInfoAction->getWapInfoList($this->conditionTagId);

// 	    $infoListq = array();
// 		if($this->conditionTagId !=\haibao\cms\business\control\PublishController::NO_STRUCTURE_TAG){
// 			$dataCondition['isTag'] = true;
// 		}else{
// 			$dataCondition['isTag'] = false;
// 		}
// 		$dataCondition['isProImage'] = true;
// 		$dataCondition['isImageColl'] = true;
// 		$dataCondition['proImageLevel'] = \haibao\cms\model\data\ArticleProImage::Level_FOUR;
// 		$dataCondition['source'] = __CLASS__.$this->conditionTagId;
// 		list($infos,$articleInfoArr,$articleProImage,$imageCollInfoArr,$collProImage,$articleTagArr,$collTagArr,$getArticleInTags) = $this->getArticleControl(7,$dataCondition);
		
	    $infoList = array();
		$topTags = self::getTopTags();
		if($infos){
			foreach($infos as $key=>$info){
				$info = $info->toArray();
				$info['PublishTime'] = $this->formatDate($info['PublishTime']->format('Y-m-d H:i:s'));
				if($info['InfoType'] == \haibao\cms\model\data\WapInfo::INFO_TYPE_ARTICLE){
					foreach ($articleInfoArr as $articleInfo){
						if($articleInfo->Id == $info['InfoId']){
							$info['Title'] = $articleInfo->ShortTitle;
							break;
						}
					}
					$domainName = 'www';
					if(isset($articleTagArr[$info['InfoId']]['ParentId'])){
						$parentId = $articleTagArr[$info['InfoId']]['ParentId'];
						if(isset($topTags[$parentId]['domain'])){
							$domainName = $topTags[$parentId]['domain'];
						}
					}
					$info['Channel'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/'.$domainName.'/articlem/'.$info['PublishId'].'.htm';
					$info['ProImage'] =  isset($articleProImage[$info['InfoId']][\haibao\cms\model\data\ArticleProImage::Level_FOUR])?$articleProImage[$info['InfoId']][\haibao\cms\model\data\ArticleProImage::Level_FOUR]:
					(isset($articleProImage[$info['InfoId']][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$articleProImage[$info['InfoId']][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
				
				}elseif($info['InfoType'] == \haibao\cms\model\data\WapInfo::INFO_TYPE_IMAGE_COLLECTIVE){
					foreach ($imageCollInfoArr as $collInfo){
						if($collInfo->Id == $info['InfoId']){
							$info['Title'] = $collInfo->ShortTitle;
							break;
						}
					}
					$domainName = 'www';
					if(isset($collTagArr[$info['InfoId']]['ParentId'])){
						$parentId = $collTagArr[$info['InfoId']]['ParentId'];
						if(isset($topTags[$parentId]['domain'])){
							$domainName = $topTags[$parentId]['domain'];
						}
					}
					$info['Channel'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/'.$domainName.'/pics/'.$info['InfoId'].'.htm';
					$info['ProImage'] =  isset($collProImage[$info['InfoId']][\haibao\cms\model\data\ArticleProImage::Level_FOUR])?$collProImage[$info['InfoId']][\haibao\cms\model\data\ArticleProImage::Level_FOUR]:
					(isset($collProImage[$info['InfoId']][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$collProImage[$info['InfoId']][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
				
				}elseif($info['InfoType'] == \haibao\cms\model\data\WapInfo::INFO_TYPE_VIDEO){
				    $info['Channel'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/v/play/'.$info['InfoId'].'/';
				    $info['ProImage'] =  isset($videoProImages[$info['InfoId']])?$videoProImages[$info['InfoId']]:'';
				}
				/*当 parentId为11280 ，美剧TV 判断是否为预告，片花，插曲  1为是，2为否*/
				if(isset($getArticleInTags[$info['InfoId']]) && $getArticleInTags[$info['InfoId']]){
				    $info['InfoChannel'] = 1;
				}else{
				    $info['InfoChannel'] = 2;
				}
				array_push($infoList,$info);
			}
		}
		$this->assign('infoList',$infoList);
	}
	
}